<?php
class News_management extends MY_Controller{
	
 	function __construct()
    {
    	parent::__construct();
    }
	function _remap($method,$params = array())
	{
	   $this->_admin_remap($method,$params);	   
	}
	function index(){    	
		$this->load->model('Language');
		$data = array();
		foreach($this->Language->get_all() as $row){
			$data['langList'][$row['language_code']] = $row['language_name'];
		}
    	$this->_setLayout('admin/news_management','admin_layout',$data);
    }
	function add_news_grid(){
    	$callback = $_REQUEST['callback'];
    	$jsonOutPut = '';
		try{
	    	$data = json_decode ($_GET['data'],true);
	    	if(empty($data)){
	    		$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not load'));	    			    		
	    	}
	    	else{
		    	$this->load->model('News');
		    	if(isset($data['id'])){
		    		$this->News->update($data,$data['id']);
		    		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','data'=>$this->News->get_row_for_grid($data['id'])));
		    	}	
		    	else{	
		    		$id = $this->News->insert($data);
		    		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','data'=>$this->News->get_row_for_grid($id)));
		    	}		 
	    	}
	    	   	
    	}
    	catch (Exception $e){
    		$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not load'));
    	}
    	if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}	
    }
	function delete_news(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);			
    		$this->load->model('News');
    		$this->News->delete($data);
			$jsonOutPut = json_encode(array('success'=>true));
		}
    	catch (Exception $e){
    		$jsonOutPut = json_encode(array('success'=>false));
    	}
		if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}
    }
	function news_grid(){
    	$this->load->model('News');
	    $callback = $_REQUEST['callback'];
	    $rsNews = $this->News->get_list($_GET['start'],$_GET['limit'],(isset($_GET['sort']))?$_GET['sort']:'date',(isset($_GET['dir']))?$_GET['dir']:'DESC',null,'id,title,url,date,language');
            $data = array();
            $i = 0;
	    foreach($rsNews as $row){
	    	$data[$i] = $row;
	    	$data[$i]['edit'] = '<a href="'.base_url().'admin/news_management/add_news/'.$row['id'].'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>';	    	
                $i++;
	    }            
	    // Create the output object.
            $jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','start'=>$_GET['start'],'total'=>$this->News->get_total(),'data'=>$data));
            //start output
            if ($callback) {
                header('Content-Type: text/javascript');
                echo $callback . '(' . $jsonOutPut . ');';
            } else {
                header('Content-Type: application/x-json');
                echo $jsonOutPut;
            }
    }
	function add_news($id=0)
	{
		$this->load->model('News');
		$this->load->model('Language');
		$this->load->library('helpfunc');
		$data = array('news' => (empty($id))?array():$this->News->get_by_id($id),
					  'message' => null);
		if($this->input->server('REQUEST_METHOD') == 'POST'){
			$input_data = $_POST;
			if($_FILES['image']['name'] !='')
			{
				$dir_upload = str_replace('//','/',getcwd() . '/public/upload/');
				$dir_fix = str_replace('//','/',$dir_upload.'/fix/');
				if(!empty($data['news']['image'])){
					unlink($dir_upload.$data['news']['image']);
					unlink($dir_fix.$data['news']['image']);
				}	
				$prefix = 'news';
				$file_input_tmp = $_FILES['image']['tmp_name'];
				$file_input_name = $_FILES['image']['name'];				
				$image = $this->helpfunc->copy_and_change_filename($file_input_tmp, $file_input_name,$dir_upload, $prefix);	
				$this->helpfunc->copy_and_resize($image,$dir_upload,$dir_fix,177,133);
				$input_data['image'] = $image;
			}
			if(!empty($id)&&$this->News->update($input_data,$id)){
				$data['message'] = 'Update successfully';
				$data['news'] = array_merge($data['news'],$input_data);	
			}elseif($id = $this->News->insert($input_data,$id)){
				$data['message'] = 'Update successfully';
				$data['news'] = $this->News->get_by_id($id);	
			}		
			else	
				$data['message'] = 'Update fail';
				
		}
		foreach($this->Language->get_all() as $row){
			$data['langList'][$row['language_code']] = $row['language_name']; 
		}
		$this->_setLayout('admin/edit_news', 'admin_layout', $data);
	}
}	